package com.yugao.fintech.antelope.infra.job.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yugao.fintech.antelope.base.utils.ExcelContentType;
import com.yugao.fintech.antelope.base.utils.ExcelUtils;
import com.yugao.fintech.antelope.infra.job.model.entity.JobLog;
import com.yugao.fintech.antelope.infra.job.model.rqrs.JobLogPageReq;
import com.yugao.fintech.antelope.infra.job.service.IJobLogService;
import com.yugao.fintech.antelope.base.log.OperateLog;
import com.yugao.fintech.antelope.base.log.enums.BusinessType;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.framework.core.response.Resp;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 调度日志操作处理
 * 

 */
@RestController
@RequestMapping("/job/log")
public class JobLogController {
    @Autowired
    private IJobLogService jobLogService;

    /**
     * 查询定时任务调度日志列表
     */
    @RamResource(action = "monitor:jobLog:query", name = "查询定时任务调度日志", type = ActionType.READ)
    @GetMapping("/page")
    public Resp<IPage<JobLog>> page(JobLogPageReq req) {
        IPage<JobLog> page = jobLogService.pageJobLog(req);
        return Resp.ok(page);
    }

    /**
     * 导出定时任务调度日志列表
     */
    @RamResource(action = "monitor:jobLog:export", name = "导出定时任务调度日志列表", type = ActionType.READ)
    @OperateLog(summary = "任务调度日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JobLogPageReq req) throws Exception {
        List<JobLog> list = jobLogService.listJobLog(req);
        ExcelUtils.exportExcel(response, ExcelContentType.XLS, "调度日志.xls", JobLog.class, list);
    }

    /**
     * 根据调度编号获取详细信息
     */
    @RamResource(action = "monitor:jobLog:get", name = "查询定时任务日志详情", type = ActionType.READ)
    @GetMapping(value = "/getById")
    public Resp<JobLog> getById(@RequestParam("id") Long jobLogId) {
        return Resp.ok(jobLogService.getById(jobLogId));
    }

    /**
     * 删除定时任务调度日志
     */
    @OperateLog(summary = "定时任务调度日志", businessType = BusinessType.DELETE)
    @RamResource(action = "monitor:jobLog:delete", name = "删除定时任务调度日志", type = ActionType.WRITE)
    @DeleteMapping("/delete")
    public Resp<?> delete(@RequestParam(value = "id") Long jobLogId) {
        return Resp.ok(jobLogService.deleteById(jobLogId));
    }

    /**
     * 批量删除定时任务调度日志
     * @param jobLogIds 任务日志ids
     */
    @OperateLog(summary = "定时任务调度日志", businessType = BusinessType.DELETE)
    @RamResource(action = "monitor:jobLog:delete", name = "删除定时任务调度日志", type = ActionType.WRITE)
    @DeleteMapping("/deleteBatch")
    public Resp<?> deleteBatch(@RequestParam(value = "ids") Long[] jobLogIds) {
        return Resp.ok(jobLogService.deleteByIds(jobLogIds), "删除失败");
    }


    /**
     * 清空定时任务调度日志
     */
    @RamResource(action = "monitor:jobLog:clean", name = "清空定时任务调度日志", type = ActionType.WRITE)
    @OperateLog(summary = "调度日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public Resp<?> clean() {
        jobLogService.cleanJobLog();
        return Resp.ok();
    }
}